package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.FriendRelation;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//操作用户资源的控制
@RestController
@RequestMapping("/users")
public class UserController{
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    /*
        查询某个用户的所有游记
        动作:GET
        资源:/users/{authorId}/travels
        参数:
     */
    @GetMapping("/{authorId}/travels")
    public Object queryTravels(TravelQueryObject qo) {
        //设置最后更新时间降序排列
        qo.setOrderBy("t.lastUpdateTime desc");
        PageInfo<Travel> pageInfo = travelService.queryTravels(qo);
        return pageInfo;
    }
    @GetMapping("/message")
    public Object UserMessage(Long id) {
        //设置最后更新时间降序排列
        User user=userService.UserMessage(id);
        return user;
    }
    @GetMapping("/{authorId}/myEnshrine")
    public Object queryMyEnshrine(TravelQueryObject qo) {
        //设置最后更新时间降序排列
        qo.setUserId(UserContext.getCurrentUser().getId());
        qo.setOrderBy("t.lastUpdateTime desc");
        PageInfo<Travel> pageInfo = travelService.queryMyEnshrine(qo);
        return pageInfo;
    }

    /*
        用户更新接口
        资源:/users/{id}
        动作:PUT
        参数:nickName=xx&place=xx&headImgUrl=xx&
             gender=xx&coverImgUrl=xx&sign=xx
     */
    @PutMapping("/{id}")
    public Object update(User u) {
        JSONResult result = new JSONResult();
        userService.update(u);
        result.setObj(u);
        return result;
    }

    /*
        用户注册:
        资源:/users
        动作:POST
        参数:email=xx&nickName=xx&password=xx
     */
    @PostMapping
    public Object regist(User u) {
        JSONResult result = new JSONResult();
        try {
            userService.regist(u);
            result.setObj(u); //设置返回的数据
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //添加好友
    @PostMapping("/addFriend")
    public Object addFriend(FriendRelation friendRelation) {
        friendRelation.getUser().setId(UserContext.getCurrentUser().getId());
        JSONResult result = new JSONResult();
        try {
            userService.addFriend(friendRelation);
            result.setObj("添加成功"); //设置返回的数据
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //查询当前登录用户的好友
    @GetMapping("/userFriend")
    public Object userFriend(UserQueryObject qo) {
        qo.setId(UserContext.getCurrentUser().getId());
        Long id = UserContext.getCurrentUser().getId();
        qo.setState(FriendRelation.STATE_SUCCESS);
        PageInfo pageInfo = userService.listForFriend(qo);
        return pageInfo;
    }
    //查询添加当前用户好友的用户
    @GetMapping("/applyFriend")
    public Object applyFriend(UserQueryObject qo) {
        qo.setPageSize(5);
        qo.setId(UserContext.getCurrentUser().getId());
        Long id = UserContext.getCurrentUser().getId();
        PageInfo pageInfo = userService.listApplyFriend(qo);
        return pageInfo;
    }
    //修改当前关系状态
    @PutMapping("/addFriend")
    public Object addFriend(Long id) {
        Long userId = UserContext.getCurrentUser().getId();
        JSONResult result = new JSONResult();
        try {
            userService.acceptRelation(id,userId);
            result.setObj("添加成功"); //设置返回的数据
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //收藏游记功能
    @PostMapping("/enshrine")
    public Object enshrine(Long travelId) {
        JSONResult result = new JSONResult();
        Long id = UserContext.getCurrentUser().getId();
        String s = userService.enshrineFunction(travelId, id);
        result.setObj(s); //设置返回的数据

        return result;
    }
    //查询是否已经收藏进行回显
    @GetMapping("/enshrine/relation")
    public Object enshrineRelation(Long travelId,Boolean state) {
        JSONResult result = new JSONResult();
        try {
            userService.enshrineRelation(travelId,UserContext.getCurrentUser().getId());
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //点赞功能
    @PostMapping("/like")
    public Object like(Long travelId) {
        JSONResult result = new JSONResult();
        Long id = UserContext.getCurrentUser().getId();
        String s = userService.likeFunction(travelId, id);
        result.setObj(s); //设置返回的数据

        return result;
    }
    //查询是否已经点赞进行回显
    @GetMapping("/like/relation")
    public Object likeRelation(Long travelId,Boolean state) {
        JSONResult result = new JSONResult();
        try {
            userService.likeRelation(travelId,UserContext.getCurrentUser().getId());
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //关注功能
    @PostMapping("/attention")
    public Object attention(Long attentionId) {
        JSONResult result = new JSONResult();
        Long id = UserContext.getCurrentUser().getId();
        String s = userService.attention(attentionId, id);
        result.setObj(s); //设置返回的数据

        return result;
    }
    //关注功能
    @GetMapping("/attentionState")
    public Object attentionState(Long attentionId) {
        JSONResult result = new JSONResult();
        Long id = UserContext.getCurrentUser().getId();
        try {
            userService.attentionState(attentionId, id);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }

        return result;
    }
    //关注条数回显功能
    @GetMapping("/fansNumber")
    public Object fansNumber() {
        Long id = UserContext.getCurrentUser().getId();
        Map<String,Object> map=userService.fansNumber(id);

        return map;
    }
    //好友通知数量回显功能
    @GetMapping("/FriendsNoticeNumber")
    public Object FriendsNoticeNumber() {
        Long id = UserContext.getCurrentUser().getId();
        Map<String,Object> map=userService.FriendsNoticeNumber(id);

        return map;
    }
    //评论我的游记的数量
    @GetMapping("/commentNumber")
    public Object commenteNumber() {
        Long id = UserContext.getCurrentUser().getId();
        Map<String,Object> map=userService.commenteNumber(id);

        return map;
    }
}
